package com.mdd.common.util.aliyunIdentity;

import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationResponse;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationResponseBody;
import com.aliyun.tea.TeaException;
import org.apache.commons.math3.util.Pair;

/**
 * 二要素（姓名和身份证）核验。用于比对用户填写的姓名、身份证两项信息是否属于同一用户
 */
public class CertNoTwoElementVerificationUtil {


    /**
     * <b>description</b> :
     * <p>使用凭据初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.dytnsapi20200217.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.setAccessKeyId("LTAI5t6J3udSCwjEh8kPrSTd")
                .setAccessKeySecret("5gbOEgbhHzNyrcW0meMACetgn9kY4X")
                .setEndpoint("dytnsapi.aliyuncs.com");
        return new com.aliyun.dytnsapi20200217.Client(config);
    }


    public static Pair<Boolean, String> certNoTwoElementVerification(String name, String certNo) throws Exception {
        com.aliyun.dytnsapi20200217.Client client = createClient();
        com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest certNoTwoElementVerificationRequest = new com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest()
                .setAuthCode("KV2l6uWa7T")
                .setCertName(name)
                .setCertNo(certNo);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        CertNoTwoElementVerificationResponse certNoTwoElementVerificationResponse = client.certNoTwoElementVerificationWithOptions(certNoTwoElementVerificationRequest, runtime);
        CertNoTwoElementVerificationResponseBody body = certNoTwoElementVerificationResponse.getBody();
        if (com.aliyun.teautil.Common.equalString(body.code, "OK")) {
            // 认证成功
            return new Pair<>(true, "认证成功");
        }
        return new Pair<>(false, "认证失败");
    }


    public static void main(String[] args_) throws Exception {

        com.aliyun.dytnsapi20200217.Client client = createClient();
        com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest certNoTwoElementVerificationRequest = new com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest()
                .setAuthCode("KV2l6uWa7T")
                .setCertName("岳建华")
                .setCertNo("410521199709267033");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            CertNoTwoElementVerificationResponse certNoTwoElementVerificationResponse = client.certNoTwoElementVerificationWithOptions(certNoTwoElementVerificationRequest, runtime);
            CertNoTwoElementVerificationResponseBody body = certNoTwoElementVerificationResponse.getBody();
            if (com.aliyun.teautil.Common.equalString(body.code, "OK")) {
                // 认证成功
                System.out.println("认证成功");
            } else {
                // 认证失败
                System.out.println("认证失败");
            }
            System.out.println(certNoTwoElementVerificationResponse);
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }

}
